/*
 * OcInstitutHome.java
 *
 * author: Daniel Rudigier
 * date: 20.05.2008
 * project: ocumed
 * 
 * Classes:
 *  OcInstitutHome
 *
 * Generated using:
 *  Hibernate Tools 3.2.1.GA, 20.05.2008 11:24:37
 *  and a powerfull PHP script, using PHP Version 5.2.0
 *
 */

package ocumed.persistenz.hibernate;

import java.util.List;

import ocumed.persistenz.dao.InstitutDAO;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;  

/**
 * Home object for domain model class OcInstitut.
 * @see ocumed.persistenz.hibernate.OcInstitut
 * @author Hibernate Tools
 */
public class OcInstitutHome implements InstitutDAO {

    private static final Log log = LogFactory.getLog(OcInstitutHome.class);

    private final SessionFactory sessionFactory = HibernateUtil.getSessionFactory();

    public void attachDirty(OcInstitut instance) {
        log.debug("attaching dirty OcInstitut instance");
        try {
            sessionFactory.getCurrentSession().saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            Transaction tx = sessionFactory.getCurrentSession().getTransaction();
            if (tx != null) {
                log.error("executing rollback.");
                tx.rollback();
            }
            throw re;
        }
    }

    public void attachClean(OcInstitut instance) {
        log.debug("attaching clean OcInstitut instance");
        try {
            sessionFactory.getCurrentSession().lock(instance, LockMode.NONE);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            Transaction tx = sessionFactory.getCurrentSession().getTransaction();
            if (tx != null) {
                log.error("executing rollback.");
                tx.rollback();
            }
            throw re;
        }
    }

    public void delete(OcInstitut persistentInstance) {
        log.debug("deleting OcInstitut instance");
        try {
            sessionFactory.getCurrentSession().delete(persistentInstance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            Transaction tx = sessionFactory.getCurrentSession().getTransaction();
            if (tx != null) {
                log.error("executing rollback.");
                tx.rollback();
            }
            throw re;
        }
    }

    public OcInstitut merge(OcInstitut detachedInstance) {
        log.debug("merging OcInstitut instance");
        try {
            OcInstitut result = (OcInstitut) sessionFactory.getCurrentSession()
                    .merge(detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            Transaction tx = sessionFactory.getCurrentSession().getTransaction();
            if (tx != null) {
                log.error("executing rollback.");
                tx.rollback();
            }
            throw re;
        }
    }

    public OcInstitut findById(int id) {
        log.debug("getting OcInstitut instance with id: " + id);
        try {
            OcInstitut instance = (OcInstitut) sessionFactory
                    .getCurrentSession().get(
                            "ocumed.persistenz.hibernate.OcInstitut", id);
            if (instance == null) {
                log.debug("get successful, no instance found");
            } else {
                log.debug("get successful, instance found");
            }
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            Transaction tx = sessionFactory.getCurrentSession().getTransaction();
            if (tx != null) {
                log.error("executing rollback.");
                tx.rollback();
            }
            throw re;
        }
    }

    /** 
     * commit a transaction
     */
    public void commit() {
        sessionFactory.getCurrentSession().getTransaction().commit();
    }

    /**
     * rollback a transaction
     */
    public void rollback() {
        sessionFactory.getCurrentSession().getTransaction().rollback();
    }

    /** 
     * start a transaction
     */
    public void start() {
        sessionFactory.getCurrentSession().getTransaction().begin();
    }  

    /**
     * find all OcInstitut instances
     */
    @SuppressWarnings("unchecked")
    public List<OcInstitut> findAll() {
        log.debug("finding all OcInstitut instances ");
        try {
            List<OcInstitut> results = sessionFactory
                    .getCurrentSession().createCriteria(OcInstitut.class).list();
            log.debug("finding all OcInstitut successful, result size: "
                    + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("finding all OcInstitut failed", re);
            Transaction tx = sessionFactory.getCurrentSession().getTransaction();
            if (tx != null) {
                log.error("executing rollback.");
                tx.rollback();
            }
            throw re;
        }
    }

    /**
     * persist a object, and return the generated id
     *
     * @param transientInstance
     * @return generated id 
     */
    public Integer persist(OcInstitut transientInstance) {
        log.debug("persisting OcInstitut instance");
        try {            
            Integer ret = (Integer) sessionFactory.getCurrentSession().save(transientInstance); 
            log.debug("persist successful");
            return ret;
        } catch (RuntimeException re) {
            log.error("persist failed", re);
            Transaction tx = sessionFactory.getCurrentSession().getTransaction();
            if (tx != null) {
                log.error("executing rollback.");
                tx.rollback();
            }
            throw re;
        }
    }
}
